import { configureStore } from "@reduxjs/toolkit";
// import thunk from 'redux-thunk';
import store_posts from "./modules/store_posts";
import store_task from "./modules/store_task";
import store_logo from "./modules/store_logo";
 
// 创建store
const store = configureStore({
    reducer: {
        // 属性名称 需要和切片名称一致
        posts: store_posts, 
        task: store_task, 
        logo: store_logo, 
    },
    // 使用中间件[如果我们不指定任何中间件,则默认集成了ReduxThunk,但是一旦设置,
    // 会整体替换默认值,需要手动指定thunk中间件]
    // middleware: [reduxThunk],
});

// +++++++++++++++++++++++++++++ redujs/toolkit中使用切片 +++++++++++++++++++++++++++++++++
// store实例可使用的方法
// store.getState()
// store.dispatch(action)
// store.subscribe()
// store.replaceReducer
 
console.log('reduxjs/toolkit...store...1', store)
console.log('reduxjs/toolkit...store...2', store.getState()); // {logo:{...}, task:{...}, todos:[...]}
store.dispatch({ type: "task/increment" });
// Selector 函数可以从 store 状态树中提取指定的片段。
const selectCounterValue = (state) => state.task;
console.log('reduxjs/toolkit...store...3',selectCounterValue(store.getState()));

export default store;

// +++++++++++++++++++++++++++++ 不切片使用redujs/toolkit +++++++++++++++++++++++++++++++++
const initialState = { value: 0 };
function counterReducer(state = initialState, action) {
  // 检查 reducer 是否关心这个 action
  if (action.type === "counter/increment") {
    return {
      ...state,
      value: state.value + 1,
    };
  }
  // 返回原来的 state 不变
  return state;
}

const storeOne = configureStore({ reducer: counterReducer });
console.log(storeOne.getState()); // {value: 0}
storeOne.dispatch({ type: "counter/increment" });
console.log(storeOne.getState()); // {value: 1}
